package com.liuguang.realgateway.filter;

import com.liuguang.common.enums.CommonResultEnum;
import com.liuguang.common.enums.RedisPreFixEnum;
import com.liuguang.common.pojo.CommonResult;
import com.liuguang.realgateway.service.ValidateCodeService;
import com.liuguang.realgateway.utils.GatewayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.resource.ResourceWebHandler;

/**
 * @ClassName: ValidateCodeFilterFactory
 * @Description：某些特定的过滤器可以再配置文件进行指定，
 * 容器启动，RouteDefinitionRouteLocator 会通过配置文件中过滤器名，在 GatewayFilterFactory 的map中查找类然后调用 apply 方法
 * 过滤器的创建方式必须实现 GatewayFilterFactory 接口。
 * @Author: wanghr
 * Date: 2:56 PM 2020/10/29
 * Version: 1.0
 */
@Component
public class ValidateCodeFilterFactory extends AbstractGatewayFilterFactory<Object> {
    private static final Logger logger = LoggerFactory.getLogger(ValidateCodeFilterFactory.class);

    @Autowired
    private ValidateCodeService validateCodeService;

    private String VALIDATE_PATH = "/api/user/login";

    @Override
    public GatewayFilter apply(Object config) {
        logger.info("进入ValidateCodeFilterFactory");

        return (exchange, chain) -> {
            logger.info("1");
            ServerHttpRequest request = exchange.getRequest();
            ServerHttpResponse response = exchange.getResponse();
            String path = request.getURI().getPath();
            // 不是登录接口不需要进行验证码验证
            if (!org.apache.commons.lang3.StringUtils.equals(path, VALIDATE_PATH)) {
                chain.filter(exchange);
            }
            logger.info("制造异常，查看结果");
            int a = 1 / 0;

            String validateCode = request.getQueryParams().getFirst("validateCode");
            if (!StringUtils.isEmpty(validateCode)) {
//                String username = request.getQueryParams().getFirst("username");
                String codeKey = request.getQueryParams().getFirst("codeKey");
                if (validateCodeService.validate(codeKey, validateCode)) {
                    chain.filter(exchange);
                }else {
                    return GatewayUtil.feedbackFriendly(response, "验证码验证失败");
                }
            } else {
                return GatewayUtil.feedbackFriendly(response, "验证码必填项不能为空");
            }
            return chain.filter(exchange);
        };
    }

}
